package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zk2/internal/dto"
	"zk2/internal/service"
)

type PersonHandler struct {
	personService service.PersonServiceImpl
}

func NewPersonHandler() *PersonHandler {
	return &PersonHandler{
		service.NewPersonService(),
	}
}
func (p *PersonHandler) List(c *gin.Context) {
	var personReq dto.PersonReq
	if err := c.ShouldBindJSON(&personReq); err != nil {
		// 返回错误信息
		// gin.H封装了生成json数据的工具
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	list, err := p.personService.List(personReq)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "我的联系人列表查询失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "我的联系人列表查询成功",
		"data": list,
	})
	return
}
